package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Resource
    private ListFeignClient listFeignClient;

    @GetMapping("/list")
    public String list(SearchParam searchParam, Model model){
        SearchResponseVo vo = listFeignClient.search(searchParam);
        //品牌集合
        model.addAttribute("trademarkList",vo.getTrademarkList());
        //构建urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //排序
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //处理品牌条件回显
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //处理平台属性条件回显
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("goodsList",vo.getGoodsList());
        //当前页
        model.addAttribute("pageNo",vo.getPageNo());
        //总页数
        model.addAttribute("totalPages",vo.getTotalPages());
        model.addAttribute("attrsList",vo.getAttrsList());
        model.addAttribute("searchParam",searchParam);
        return "list/index";
    }

    private List<Map> buildPropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0){
            List<Map> listMap = Arrays.stream(props).map(prop -> {
                Map hashMap = new HashMap();
                String[] split = prop.split(":");
                hashMap.put("attrId", split[0]);
                hashMap.put("attrName", split[2]);
                hashMap.put("attrValue", split[1]);
                return hashMap;
            }).collect(Collectors.toList());
            return listMap;
        }
        return null;
    }

    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            String[] split = trademark.split(":");
            return "品牌：" + split[1];
        }
        return null;
    }

    private Map buildOrderMap(SearchParam searchParam) {
        Map hashMap = new HashMap<>();
        String order = searchParam.getOrder();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            hashMap.put("type",split[0]);
            hashMap.put("sort",split[1]);
        } else {
            hashMap.put("type","1");
            hashMap.put("sort","asc");
        }

        return hashMap;
    }

    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder();
        //关键词
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)){
            stringBuilder.append("keyword=").append(keyword);
        }
        //三级分类
        Long category1Id = searchParam.getCategory1Id();
        if (category1Id != null){
            stringBuilder.append("category1Id=").append(category1Id);
        }
        Long category2Id = searchParam.getCategory2Id();
        if (category2Id != null){
            stringBuilder.append("category2Id=").append(category2Id);
        }
        Long category3Id = searchParam.getCategory3Id();
        if (category3Id != null){
            stringBuilder.append("category3Id=").append(category3Id);
        }
        //品牌
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            stringBuilder.append("&trademark=").append(trademark);
        }
        //平台属性值
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0){
            for (String prop : props) {
                stringBuilder.append("&props=").append(prop);
            }
        }
        return "/list?" + stringBuilder.toString();
    }
}
